package com.zzyk.microCloud.controller;


import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.Result;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.AlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wss_PC
 * @DESCRIPTION 报警信息控制器
 * @create 2020-6-15 15:55
 */
@RestController
@CrossOrigin(allowCredentials = "true")
@RequestMapping("/alarm")
public class AlarmController extends BaseController {
    @Autowired
    private AlarmInfoService alarmInfoService;

    /**
     * 获取告警近期通知 五天内的报警
     *
     * @param type
     * @return
     */
    @GetMapping("/recentAlertNotification")
    public Message recentAlertNotification(Integer type,
                                           @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                           Integer methodType
    ) throws UserException {
        SysUser sysUser = currentUser();
        return alarmInfoService.selectRecentAlertNotification(type, sysUser.getEnterprise(), pageIndex, pageSize, methodType);
    }

    /**
     * 历史试图 告警设备类型
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getHistoryAlarmDeviceType")
    public Message getHistoryAlarmDeviceType(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.selectHistoryAlarmDeviceType(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList())).addData("count", alarmInfoService.getCountHistoryAlarmDeviceType(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 历史视图 告警原因分布
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmCauseDistribution")
    public Message getAlarmCauseDistribution(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.selectAlarmCauseDistribution(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 历史试图 告警处理信息
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmHandlingInformation")
    public Message getAlarmHandlingInformation(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.selectAlarmHandlingInformation(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 告警管理历史视图里告警统计
     *
     * @param dateStr
     * @return
     */
    @GetMapping("/getAlarmStatistics")
    public Message getAlarmStatistics(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.selectAlarmStatistics(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 根据设备类型分组统计各个类型报警设备数量
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmDeviceType")
    public Message getAlarmDeviceType(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success()
                .addData("data", alarmInfoService.selectAlarmDeviceType(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList())).addData("count", alarmInfoService.getAlarmCountBy(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 查询告警原因分布
     *
     * @param dateStr
     * @return
     * @throws UserException
     */
    @GetMapping("/getAlarmNumberGroupByContent")
    public Message getAlarmNumberGroupByContent(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.findAlarmNumberGroupByContent(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 查询某一天的各个告警状态的数量
     *
     * @return
     */
    @GetMapping("/countToDayAlarmNumber")
    public Message countToDayAlarmNumber(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.countToDayAlarmNumber(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 统计传入时间的今天和昨天每个小时的报警数量
     *
     * @param dateStr
     * @return
     */
    @GetMapping("/getCountAlarmsByHour")
    public Message getCountAlarmsByHour(String dateStr) throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.findCountAlarmsByHour(dateStr, sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 获取当月告警信息 当月每天的告警数量
     *
     * @return
     */
    @GetMapping("/getToMonthAlarmData")
    public Message getToMonthAlarmData() throws UserException {
        SysUser sysUser = currentUser();
        return Message.success().addData("data", alarmInfoService.selectToMonthAlarmData(sysUser.getEnterprise(),sysUser.getDeviceList()));
    }

    /**
     * 多条件查询报警信息 前十条
     *
     * @param dId
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    @GetMapping("/findAlarmByParamsTop10")
    public Result findAlarmByParamsTop10(Long dId, String beginTime, String endTime, Long status) {
        try {
            return Result.ok(alarmInfoService.selectAlarmByParamsTop10(0L, dId, beginTime, endTime, status));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 多条件查询报警信息
     *
     * @param dId
     * @param beginTime
     * @param endTime
     * @param status
     * @return
     */
    @GetMapping("/findAlarmByParams")
    public Message findAlarmByParams(Long dId, String beginTime, String endTime, Long status, Integer pageIndex, Integer pageSize) throws UserException {
        SysUser user = currentUser();
        try {
            return alarmInfoService.selectAlarmByParams(user.getEnterprise().longValue(), dId, beginTime, endTime, status, null, pageIndex, pageSize, user.getDeviceList());
        } catch (Exception e) {
            e.printStackTrace();
            return Message.failed("查询失败");
        }
    }

}